package com.credit.jumu.calculator.activity;

import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.credit.jumu.calculator.R;
import com.credit.jumu.calculator.adapter.OtherResultListAdapter;
import com.credit.jumu.calculator.base.BaseActivity;
import com.credit.jumu.calculator.bean.OtherResultInfo;
import com.credit.jumu.calculator.utils.AverageCapitalPlusInterestUtils;
import com.credit.jumu.calculator.utils.AverageCapitalUtils;
import com.credit.jumu.calculator.utils.LogUtils;
import com.credit.jumu.calculator.utils.StringHandleUtil;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/23/023.
 */
@EActivity(R.layout.activity_other_result)
public class OtherResultActivity extends BaseActivity {
	@ViewById
	TextView barheight;
	@ViewById
	View topheight;
	@ViewById
	ListView otherresultlist;
	@ViewById
	LinearLayout taxlayout1;//显示隐藏的布局
	LinearLayout otherhead;//布局
	View header;
	OtherResultListAdapter otherResultListAdapter;
	private List<OtherResultInfo> mlistInfo = new ArrayList<OtherResultInfo>();
	int taxlayouttop;
	int taxheight;
	double allpic;//总金额
	double commlilvs;//利率

	double allmoney1;//混合一
	double lilv1;//利率
	double allmoney2;//混合一
	double lilv2;//利率

//	intent.putExtra("allmoney1", allmoney1d);
//			intent.putExtra("lilv1", lilv1d);//
//			intent.putExtra("allmoney2", allmoney2d);//
//			intent.putExtra("lilv2", lilv2d);//
//			intent.putExtra("blendyearsd", blendyearsd);//

	int commyear;//年限
	int type;//方式
	int page;//
	ImageView back;

	TextView titlett, everyMonth, allshow, needInterest, allneedmoney, allshow2;//标题,每月月供,分类显示,累计利息,累计还款


	@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
	@AfterViews
	public void initView() {

		Intent intent = getIntent();
		if (null == intent) {
			this.finish();
			return;
		}
		page = intent.getIntExtra("page", 0);
		if (page == 1) {
			allpic = intent.getDoubleExtra("allpic", 0);
			commlilvs = intent.getDoubleExtra("commlilvs", 0);
			commyear = intent.getIntExtra("needyeard", 0);
			type = intent.getIntExtra("type", 0);
			if (allpic <= 0 || commlilvs <= 0 || commyear <= 0) {
				this.finish();
				return;
			}
		} else {
			allmoney1 = intent.getDoubleExtra("allmoney1", 0);
			lilv1 = intent.getDoubleExtra("lilv1", 0);
			allmoney2 = intent.getDoubleExtra("allmoney2", 0);
			lilv2 = intent.getDoubleExtra("lilv2", 0);
			commyear = intent.getIntExtra("blendyearsd", 0);
			type = intent.getIntExtra("type", 0);
		}


		RelativeLayout.LayoutParams Params1 = (RelativeLayout.LayoutParams) barheight.getLayoutParams();
		Params1.height = getNavigationBarHeight();
		barheight.setLayoutParams(Params1);

		RelativeLayout.LayoutParams Params2 = (RelativeLayout.LayoutParams) topheight.getLayoutParams();
		Params2.height = getStatusBarHeight();
		topheight.setLayoutParams(Params2);

		toAllWindow();
		header = LayoutInflater.from(this).inflate(R.layout.layout_otherhead_result, null);
		//TextView everyMonth, allshow, needInterest, allneedmoney;
		everyMonth = header.findViewById(R.id.everyMonth);
		allshow = header.findViewById(R.id.allshow);
		allshow2 = header.findViewById(R.id.allshow2);
		needInterest = header.findViewById(R.id.needInterest);
		everyMonth = header.findViewById(R.id.everyMonth);
		allneedmoney = header.findViewById(R.id.allneedmoney);
		otherhead = header.findViewById(R.id.otherhead);
		titlett = header.findViewById(R.id.titlett);
		back = header.findViewById(R.id.back);
		back.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				OtherResultActivity.this.finish();
			}
		});
		otherhead.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);

		int width = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int height = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		otherhead.measure(width, height);
		otherhead.getMeasuredWidth(); // 获取宽度
		otherhead.getMeasuredHeight(); // 获取高度


		taxlayouttop = otherhead.getTop();
		taxheight = otherhead.getMeasuredHeight();

		otherresultlist.addHeaderView(header);
		otherResultListAdapter = new OtherResultListAdapter(mlistInfo, this);
		otherresultlist.setAdapter(otherResultListAdapter);
		setData();
		getScrollHeight();
	}

	private void setData() {
		if (page == 1) {//商业险
			allshow2.setVisibility(View.GONE);
			if (type == 0) {//等额本息

				titlett.setText("每月月供");
				double invest = allpic * 10000; // 本金
				int month = commyear * 12;
				double yearRate = commlilvs / 100; // 年利率
				double perMonthPrincipalInterest = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(invest, yearRate, month);
				System.out.println("等额本息---每月还款本息：" + perMonthPrincipalInterest);
				Map<Integer, BigDecimal> mapInterest = AverageCapitalPlusInterestUtils.getPerMonthInterest(invest, yearRate, month);
				System.out.println("等额本息---每月还款利息：" + mapInterest);
				Map<Integer, BigDecimal> mapPrincipal = AverageCapitalPlusInterestUtils.getPerMonthPrincipal(invest, yearRate, month);
				System.out.println("等额本息---每月还款本金：" + mapPrincipal);
				double count = AverageCapitalPlusInterestUtils.getInterestCount(invest, yearRate, month);
				System.out.println("等额本息---总利息：" + count);
				double principalInterestCount = AverageCapitalPlusInterestUtils.getPrincipalInterestCount(invest, yearRate, month);
				System.out.println("等额本息---应还本息总和：" + principalInterestCount);

				everyMonth.setText(perMonthPrincipalInterest + "");
				allshow.setText("贷款总额 " + StringHandleUtil.toOnlyHavaTwo(allpic) + "万元  利率" + StringHandleUtil.toOnlyHavaTwo(commlilvs) + "%   贷款年限" + commyear + "年");
				needInterest.setText(count + "");
				allneedmoney.setText(principalInterestCount + "");

				for (int i = 0; i < mapInterest.size(); i++) {
					OtherResultInfo otherinfo = new OtherResultInfo();
					otherinfo.setPhase("第" + (i + 1) + "期");
					otherinfo.setMonthMoney(perMonthPrincipalInterest + "");
					otherinfo.setPrincipal(mapPrincipal.get(i + 1).toString());
					otherinfo.setInterest(mapInterest.get(i + 1).toString());
					mlistInfo.add(otherinfo);
				}
				otherResultListAdapter.notifyDataSetChanged();

			} else {
				titlett.setText("首次还款");
				double invest = allpic * 10000; // 本金
				int month = commyear * 12;
				double yearRate = commlilvs / 100; // 年利率
				Map<Integer, Double> getPerMonthPrincipalInterest = AverageCapitalUtils.getPerMonthPrincipalInterest(invest, yearRate, month);
				System.out.println("等额本金---每月本息：" + getPerMonthPrincipalInterest);
				double benjin = AverageCapitalUtils.getPerMonthPrincipal(invest, month);
				System.out.println("等额本金---每月本金:" + benjin);
				Map<Integer, Double> mapInterest = AverageCapitalUtils.getPerMonthInterest(invest, yearRate, month);
				System.out.println("等额本金---每月利息:" + mapInterest);

				double count = AverageCapitalUtils.getInterestCount(invest, yearRate, month);
				System.out.println("等额本金---总利息：" + count);

				everyMonth.setText(getPerMonthPrincipalInterest.get(1) + "");//首次
				allshow.setText("贷款总额 " + StringHandleUtil.toOnlyHavaTwo(allpic) + "万元  利率" + StringHandleUtil.toOnlyHavaTwo(commlilvs) + "%   贷款年限" + commyear + "年");
				needInterest.setText(count + "");
				allneedmoney.setText(count + (allpic) + "");
				for (int i = 0; i < mapInterest.size(); i++) {
					OtherResultInfo otherinfo = new OtherResultInfo();
					otherinfo.setPhase("第" + (i + 1) + "期");
					otherinfo.setMonthMoney(getPerMonthPrincipalInterest.get(i + 1).toString());
					otherinfo.setPrincipal(benjin + "");
					otherinfo.setInterest(mapInterest.get(i + 1).toString());
					mlistInfo.add(otherinfo);
				}
				otherResultListAdapter.notifyDataSetChanged();
			}

		} else {//混合
			allshow2.setVisibility(View.VISIBLE);
			if (type != 0) {//等额本金
				titlett.setText("首次还款");
				double invest = allmoney1 * 10000; // 本金
				int month = commyear * 12;
				double yearRate = lilv1 / 100; // 年利率

				Map<Integer, Double> getPerMonthPrincipalInterest = AverageCapitalUtils.getPerMonthPrincipalInterest(invest, yearRate, month);
				System.out.println("等额本金---每月本息：" + getPerMonthPrincipalInterest);
				double benjin = AverageCapitalUtils.getPerMonthPrincipal(invest, month);
				System.out.println("等额本金---每月本金:" + benjin);
				Map<Integer, Double> mapInterest = AverageCapitalUtils.getPerMonthInterest(invest, yearRate, month);
				System.out.println("等额本金---每月利息:" + mapInterest);

				double count = AverageCapitalUtils.getInterestCount(invest, yearRate, month);
				System.out.println("等额本金---总利息：" + count);

				double invest1 = allmoney2 * 10000; // 本金
				int month1 = commyear * 12;
				double yearRate1 = lilv2 / 100; // 年利率

				Map<Integer, Double> getPerMonthPrincipalInterest1 = AverageCapitalUtils.getPerMonthPrincipalInterest(invest1, yearRate1, month1);
				System.out.println("等额本金1---每月本息：" + getPerMonthPrincipalInterest1);
				double benjin1 = AverageCapitalUtils.getPerMonthPrincipal(invest1, month1);
				System.out.println("等额本金1---每月本金:" + benjin1);
				Map<Integer, Double> mapInterest1 = AverageCapitalUtils.getPerMonthInterest(invest1, yearRate1, month1);
				System.out.println("等额本金1---每月利息:" + mapInterest1);

				double count1 = AverageCapitalUtils.getInterestCount(invest1, yearRate1, month1);
				System.out.println("等额本金1---总利息：" + count1);

				allshow.setText("公积金:贷款总额 " + StringHandleUtil.toOnlyHavaTwo(allmoney1) + "万元  利率" + StringHandleUtil.toOnlyHavaTwo(lilv1) + "%   贷款年限" + commyear + "年");
				allshow2.setText("商业:贷款总额 " + StringHandleUtil.toOnlyHavaTwo(allmoney2) + "万元  利率" + StringHandleUtil.toOnlyHavaTwo(lilv2) + "%   贷款年限" + commyear + "年");
				everyMonth.setText(StringHandleUtil.toOnlyHavaTwo(getPerMonthPrincipalInterest.get(1) + getPerMonthPrincipalInterest1.get(1)) + "");//首次
//				allshow.setText("贷款总额 " + allpic + "万元  利率" + commlilvs + "%   贷款年限" + commyear + "年");
				needInterest.setText(StringHandleUtil.toOnlyHavaTwo(count + count1) + "");//利息
				allneedmoney.setText(StringHandleUtil.toOnlyHavaTwo((allmoney1 + allmoney2) * 10000 + count + count1) + "");//所有

				for (int i = 0; i < mapInterest.size(); i++) {
					OtherResultInfo otherinfo = new OtherResultInfo();
					otherinfo.setPhase("第" + (i + 1) + "期");
					otherinfo.setMonthMoney(StringHandleUtil.toOnlyHavaTwo((getPerMonthPrincipalInterest.get(i + 1) + getPerMonthPrincipalInterest1.get(i + 1))));
					otherinfo.setPrincipal(StringHandleUtil.toOnlyHavaTwo((benjin + benjin1)) + "");
					otherinfo.setInterest(StringHandleUtil.toOnlyHavaTwo((mapInterest.get(i + 1) + mapInterest1.get(i + 1))) + "");
					mlistInfo.add(otherinfo);
				}
				otherResultListAdapter.notifyDataSetChanged();

			} else {//混合等额本息

				titlett.setText("每月月供");
				double invest = allmoney1 * 10000; // 本金
				int month = commyear * 12;
				double yearRate = lilv1 / 100; // 年利率
				double perMonthPrincipalInterest = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(invest, yearRate, month);
				System.out.println("等额本息---每月还款本息：" + perMonthPrincipalInterest);
				Map<Integer, BigDecimal> mapInterest = AverageCapitalPlusInterestUtils.getPerMonthInterest(invest, yearRate, month);
				System.out.println("等额本息---每月还款利息：" + mapInterest);
				Map<Integer, BigDecimal> mapPrincipal = AverageCapitalPlusInterestUtils.getPerMonthPrincipal(invest, yearRate, month);
				System.out.println("等额本息---每月还款本金：" + mapPrincipal);
				double count = AverageCapitalPlusInterestUtils.getInterestCount(invest, yearRate, month);
				System.out.println("等额本息---总利息：" + count);
				double principalInterestCount = AverageCapitalPlusInterestUtils.getPrincipalInterestCount(invest, yearRate, month);
				System.out.println("等额本息---应还本息总和：" + principalInterestCount);

				double invest1 = allmoney2 * 10000; // 本金
				int month1 = commyear * 12;
				double yearRate1 = lilv2 / 100; // 年利率

				double perMonthPrincipalInterest1 = AverageCapitalPlusInterestUtils.getPerMonthPrincipalInterest(invest1, yearRate1, month1);
				System.out.println("等额本息11---每月还款本息：" + perMonthPrincipalInterest1);
				Map<Integer, BigDecimal> mapInterest1 = AverageCapitalPlusInterestUtils.getPerMonthInterest(invest1, yearRate1, month1);
				System.out.println("等额本息11---每月还款利息：" + mapInterest1);
				Map<Integer, BigDecimal> mapPrincipal1 = AverageCapitalPlusInterestUtils.getPerMonthPrincipal(invest1, yearRate1, month1);
				System.out.println("等额本息11---每月还款本金：" + mapPrincipal1);
				double count1 = AverageCapitalPlusInterestUtils.getInterestCount(invest1, yearRate1, month1);
				System.out.println("等额本息11---总利息：" + count1);
				double principalInterestCount1 = AverageCapitalPlusInterestUtils.getPrincipalInterestCount(invest1, yearRate1, month1);
				System.out.println("等额本息11---应还本息总和：" + principalInterestCount1);

				everyMonth.setText(StringHandleUtil.toOnlyHavaTwo(perMonthPrincipalInterest + perMonthPrincipalInterest1) + "");
				allshow.setText("公积金:贷款总额 " + StringHandleUtil.toOnlyHavaTwo(allmoney1) + "万元  利率" + StringHandleUtil.toOnlyHavaTwo(lilv1) + "%   贷款年限" + commyear + "年");
				allshow2.setText("商业:贷款总额 " + StringHandleUtil.toOnlyHavaTwo(allmoney2) + "万元  利率" + StringHandleUtil.toOnlyHavaTwo(lilv2) + "%   贷款年限" + commyear + "年");

				needInterest.setText(StringHandleUtil.toOnlyHavaTwo(count + count1 )+ "");
				allneedmoney.setText(StringHandleUtil.toOnlyHavaTwo(principalInterestCount + principalInterestCount1) + "");

				for (int i = 0; i < mapInterest.size(); i++) {
					OtherResultInfo otherinfo = new OtherResultInfo();
					otherinfo.setPhase("第" + (i + 1) + "期");
					otherinfo.setMonthMoney(perMonthPrincipalInterest + perMonthPrincipalInterest1 + "");
					otherinfo.setPrincipal(StringHandleUtil.toOnlyHavaTwo((mapPrincipal.get(i + 1).add(mapPrincipal1.get(i + 1))).doubleValue()));
					otherinfo.setInterest(StringHandleUtil.toOnlyHavaTwo((mapInterest.get(i + 1).add(mapInterest1.get(i + 1))).doubleValue()));
					mlistInfo.add(otherinfo);
				}
				otherResultListAdapter.notifyDataSetChanged();


			}
		}
	}

	public int getScrollHeight() {
		otherresultlist.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {

			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				if (taxheight + header.getTop() - getStatusBarHeight() < 0) {
					topheight.setVisibility(View.VISIBLE);
					taxlayout1.setVisibility(View.VISIBLE);
				} else {
					topheight.setVisibility(View.GONE);
					taxlayout1.setVisibility(View.GONE);
				}
			}
		});
		return 0;
	}
}
